﻿using System.Diagnostics;
using System.Windows.Controls;
using System.Windows;
using SimpleDbBrowser.ViewModel;

namespace SimpleDbBrowser.UserControls
{
    /// <summary>
    /// Interaction logic for DeleteDomainUserControl.xaml
    /// </summary>
    public partial class DeleteDomainUserControl : UserControl
    {
        public DeleteDomainUserControl()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles a click on the delete domain button.
        /// </summary>
        /// <param name="sender">
        /// the source of the event.
        /// </param>
        /// <param name="e">
        /// The event's arguments.
        /// </param>
        /// <remarks>
        /// It's assumed that this runs _before_ the bound command is executed!
        /// </remarks>
        private void HandleDeleteDomainClicked(object sender, System.Windows.RoutedEventArgs e)
        {
            var vm = (DeleteDomainViewModel)this.DataContext;

            vm.DeletionConfirmed = this.DeletionConfirmed(vm);
        }

        /// <summary>
        /// Gets a value indicating whether the user has confirmed that the domain deletion should take place.
        /// </summary>
        /// <param name="thisVm">
        /// The view model that this control is bound to.
        /// </param>
        /// <returns>
        /// True if the user confirms that the domain should be deleted, otherwise false.
        /// </returns>
        private bool DeletionConfirmed(DeleteDomainViewModel thisVm)
        {
            var wnd = Window.GetWindow(this);
            Debug.Assert(wnd != null);
            var result = MessageBox.Show(wnd,
                string.Format("Do you wish to delete the \"{0}\" domain?", thisVm.SelectedDomain),
                Application.Current.MainWindow.Title, // This seems pretty ghetto
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            return result == MessageBoxResult.Yes;
        }
    }
}
